package com.sinopec.android.net;

import java.util.ArrayList;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;

import com.sinopec.android.bean.AddressBean;
import com.sinopec.android.bean.AddressBook;
import com.sinopec.android.bean.Agenda;
import com.sinopec.android.bean.AgendaBean;
import com.sinopec.android.bean.AttDoc;
import com.sinopec.android.bean.BusinessTripBean;
import com.sinopec.android.bean.Conference;
import com.sinopec.android.bean.ConferenceBean;
import com.sinopec.android.bean.DispathBasic;
import com.sinopec.android.bean.DocumentBean;
import com.sinopec.android.bean.KqglBasic;
import com.sinopec.android.bean.ProcessIdea;
import com.sinopec.android.bean.ResultData;
import com.sinopec.android.bean.SubmitBusinessTripBean;
import com.sinopec.android.bean.Thing;
import com.sinopec.android.bean.ThingBean;
import com.sinopec.android.bean.UserInfo;
import com.sinopec.android.constant.Constants;
import com.sinopec.android.util.StringUtil;

import android.content.Context;



/**
 * 【解析类】
 * @ClassName:ParseManger
 * @Description:TODO
 * @author liuwei
 * @date 2012-3-8下午01:48:45
 *
 */
public class ParseManger {
	
	/**
	 * @Title :getLoginUserInfo
	 * @Description :获取登录用户的返回信息
	 * @params @param response
	 * @params @param mContext
	 * @params @return
	 * @return ResultData
	 * @throws
	 * 
	 */
	@SuppressWarnings("rawtypes")
	public static ResultData getLoginUserInfo(String response, Context mContext) {
		if (response != null && !"".equals(response)) {
		} else {
			return null;
		}
		ResultData resultData = new ResultData();
		ArrayList arrayList = new ArrayList();
		try {
			JSONParser parser = new JSONParser();
			JSONObject jsonObject = (JSONObject) parser.parse(response);
			JSONObject result = (JSONObject) jsonObject.get("result");
			if (!"".equals(result.get("message"))) {
				arrayList.add(result.get("message"));
				resultData.setResultList(arrayList);
				return resultData;
			}
//			 {"result":{"errorCode":"","message":""},"userName":"亓东霞"}
			 UserInfo user = new UserInfo();
			 if(jsonObject.containsKey("userName")){
				 user.setUserName((String) jsonObject.get("userName"));
			 }
			 if(jsonObject.containsKey("accountName")){
				 user.setAccountName((String) jsonObject.get("accountName"));
			 }
			 if(jsonObject.containsKey("annouceToDo")){
				 try{
				 Constants.annouceToDo =  (Long)jsonObject.get("annouceToDo");
				 }catch (Exception e) {
					// TODO: handle exception
				}
			 }
			 if(jsonObject.containsKey("documentToDo")){
				 try{
				 Constants.documentToDo = (Long) jsonObject.get("documentToDo");
				 }catch (Exception e) {
					// TODO: handle exception
				}
			 }
			 if(jsonObject.containsKey("travelToDo")){
				 try{
				 Constants.travelToDo = (Long)jsonObject.get("travelToDo");
				 }catch (Exception e) {
					// TODO: handle exception
				}
			 }
//			accountInfo.setMemberid((String) jsonObject.get("memberId"));
//			accountInfo.setEmail((String) jsonObject.get("email"));
//			accountInfo.setCode((String) jsonObject.get("code"));
//			accountInfo.setMobile((String) jsonObject.get("mobile"));
			arrayList.add(user);
			resultData.setResultList(arrayList);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		return resultData;
	}

	public static  ResultData getAddressBookList(String response, Context mContext){
		if (response != null && !"".equals(response)) {
		} else {
			return null;
		}
		ResultData resultData = new ResultData();
		ArrayList arrayList = new ArrayList();
		try {
			JSONParser parser = new JSONParser();
			JSONObject jsonObject = (JSONObject) parser.parse(response);
			JSONObject result = (JSONObject) jsonObject.get("result");
			if (!"".equals(result.get("message"))) {
				arrayList.add(result.get("message"));
				resultData.setResultList(arrayList);
				return resultData;
			}
			
			 AddressBean  addressBean = new AddressBean();
			 ArrayList<AddressBook> addressBookList = new ArrayList<AddressBook>();
			 
			 JSONArray jsonArray = (JSONArray) jsonObject.get("resultList");
			 AddressBook addressBook = null;
		
			 
			 for (int i = 0; i < jsonArray.size(); i++) {
				JSONObject item =  (JSONObject) jsonArray.get(i);
				if(item!=null){
				 addressBook = new AddressBook();
				 addressBook.setName(String.valueOf(item.get("name")));
				 addressBook.setDept(String.valueOf(item.get("department")));
				 addressBook.setJob(String.valueOf(item.get("position")));
				 addressBook.setTel(String.valueOf(item.get("mobileNum")));
				 addressBookList.add(addressBook);
				}
			}
			
			addressBean.setAddressBookList(addressBookList);
			arrayList.add(addressBean);
			resultData.setResultList(arrayList);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		return resultData;
	}
	
	public static  ResultData  getToDoThingList(String response, Context mContext){
		if (response != null && !"".equals(response)) {
		} else {
			return null;
		}
		ResultData resultData = new ResultData();
		ArrayList arrayList = new ArrayList();
		try {
			JSONParser parser = new JSONParser();
			JSONObject jsonObject = (JSONObject) parser.parse(response);
			JSONObject result = (JSONObject) jsonObject.get("result");
			if (!"".equals(result.get("message"))) {
				arrayList.add(result.get("message"));
				resultData.setResultList(arrayList);
				return resultData;
			}
			
			 ThingBean  thingBean = new ThingBean();
			 ArrayList<Thing> thingList = new ArrayList<Thing>();
			 
			 
			 JSONArray jsonArray = (JSONArray) jsonObject.get("list");
			 Thing thing = null;
			 
			 long count = (Long) jsonObject.get("account");
//			 long count = jsonArray.size();
			 thingBean.setCount(count);
			 
			 for (int i = 0; i < jsonArray.size(); i++) {
				JSONObject item =  (JSONObject) jsonArray.get(i);
				if(item!=null){
					thing = new Thing();
					thing.setType(String.valueOf(item.get("type")));
					thing.setName(String.valueOf(item.get("name")));
					thing.setTitle(String.valueOf(item.get("title")));
					thing.setDate(String.valueOf(item.get("date")));
					thing.setUrl(String.valueOf(item.get("url")));
					thing.setBussinessID(String.valueOf(item.get("bussinessID")));
					thingList.add(thing);
				}
			}
			
			thingBean.setList(thingList);
			arrayList.add(thingBean);
			resultData.setResultList(arrayList);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		return resultData;
	}
	
	
	public static  ResultData getConferenceManagementList(String response, Context mContext){
		if (response != null && !"".equals(response)) {
		} else {
			return null;
		}
		ResultData resultData = new ResultData();
		ArrayList arrayList = new ArrayList();
		try {
			JSONParser parser = new JSONParser();
			JSONObject jsonObject = (JSONObject) parser.parse(response);
			JSONObject result = (JSONObject) jsonObject.get("result");
			if (!"".equals(result.get("message"))) {
				arrayList.add(result.get("message"));
				resultData.setResultList(arrayList);
				return resultData;
			}
			
			 ConferenceBean  conferenceBean = new ConferenceBean();
			 ArrayList<Conference> conferenceList = null;
			 
			 
			 JSONArray jsonArray = (JSONArray) jsonObject.get("list");
			 Conference conference = null;
			 if(jsonObject.containsKey("account")){
				 long count = (Long) jsonObject.get("account");
				 conferenceBean.setCount(count);
			 }
			 if(jsonObject.containsKey("weekOfYear")){
				 long week = (Long) jsonObject.get("weekOfYear");
				 conferenceBean.setWeek(week);
			 }
			 if(jsonObject.containsKey("weekInfo")){
				 conferenceBean.setWeekDate(String.valueOf(jsonObject.get("weekInfo")));
			 }
			 if(jsonArray!=null){
				 conferenceList = new ArrayList<Conference>();
				 for (int i = 0; i < jsonArray.size(); i++) {
					JSONObject item =  (JSONObject) jsonArray.get(i);
					if(item!=null){
						conference = new Conference();
						conference.setTitle(String.valueOf(item.get("title")));
						conference.setTime(String.valueOf(item.get("time")));
						conference.setAddress(String.valueOf(item.get("address")));
						conference.setLeader(String.valueOf(item.get("leader")));
						conference.setContent(String.valueOf(item.get("content")));
						conference.setHolder(String.valueOf(item.get("holder")));
						conference.setAttender(String.valueOf(item.get("attender")));
						conference.setContract(String.valueOf(item.get("contract")));
						conferenceList.add(conference);
					}
				}
			 }
			
			conferenceBean.setConferenceList(conferenceList);
			arrayList.add(conferenceBean);
			resultData.setResultList(arrayList);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		return resultData;
	}
	
	public static  ResultData  getDocumentDetail(String response, Context mContext){
		if (response != null && !"".equals(response)) {
		} else {
			return null;
		}
		ResultData resultData = new ResultData();
		ArrayList arrayList = new ArrayList();
		try {
			JSONParser parser = new JSONParser();
			JSONObject jsonObject = (JSONObject) parser.parse(response);
			JSONObject result = (JSONObject) jsonObject.get("result");
			if (!"".equals(result.get("message"))) {
				arrayList.add(result.get("message"));
				resultData.setResultList(arrayList);
				return resultData;
			}
			
			 DocumentBean  documentBean = new DocumentBean();
			 DispathBasic dispathBasic = null;
			 ArrayList<DocumentBean> documentBeanList = new ArrayList<DocumentBean>();
			 ArrayList<AttDoc>  attDocList = null;
			 AttDoc  attDoc = null;
			 ArrayList<ProcessIdea> processIdeaList = null ;
			 ProcessIdea processIdea = null;
			 if(jsonObject.containsKey("JobStepID")){
				 documentBean.setJobStepID((Long)jsonObject.get("JobStepID"));
			 }
			 if(jsonObject.containsKey("BtnType")){
				 documentBean.setBtnType((Long)jsonObject.get("BtnType"));
			 }
			 if(jsonObject.containsKey("InsID")){
				 documentBean.setInsID(String.valueOf(jsonObject.get("InsID")));
			 }
			 if(jsonObject.containsKey("nowJobStep")){
				 documentBean.setNowJobStep((Long)jsonObject.get("nowJobStep"));
			 }
			 if(jsonObject.containsKey("dispathBasic")){
				 dispathBasic = new DispathBasic();
				 JSONObject item = (JSONObject) jsonObject.get("dispathBasic");
				 if(item.containsKey("recData")){
					 dispathBasic.setRecData(String.valueOf(item.get("recData")));
				 }
				 if(item.containsKey("recNo")){
					 dispathBasic.setRecNo(String.valueOf(item.get("recNo")));
				 }
				 if(item.containsKey("docNo")){
					 dispathBasic.setDocNo(String.valueOf(item.get("docNo")));
				 }
				 if(item.containsKey("recDeport")){
					 dispathBasic.setRecDeport(String.valueOf(item.get("recDeport")));
				 }
				 if(item.containsKey("recTitle")){
					 dispathBasic.setRecTitle(String.valueOf(item.get("recTitle")));
				 }
				 if(item.containsKey("remark")){
					 dispathBasic.setRemark(String.valueOf(item.get("remark")));
				 }
				 if(item.containsKey("tileState")){
					 dispathBasic.setTileState(String.valueOf(item.get("tileState")));
				 }
				 
				 documentBean.setDispathBasic(dispathBasic);
			 }
			 
			 if(jsonObject.containsKey("AttDocList")){
				 attDocList = new ArrayList<AttDoc>();
				 JSONArray jsonArray = (JSONArray) jsonObject.get("AttDocList");
				 for (int i = 0; i < jsonArray.size(); i++) {
					 
					 JSONObject item = (JSONObject) jsonArray.get(i);
					 
					 
					 String url = String.valueOf(item.get("attFilePath"));
					 if(StringUtil.isNotEmpty(url)){
//						 String[] urlArray =  url.split(";");
//						 if(urlArray!=null && urlArray.length>1){
//							 for (int j = 0; j < urlArray.length; j++) {
//								 	attDoc = new AttDoc();
//									attDoc.setAttFilePath(urlArray[j]);
//									attDoc.setTypeName(String.valueOf(item.get("typeName")));
//									attDoc.setAttFieleName(String.valueOf(item.get("attFieleName")) + "(第"+(j+1)+"页)");
//									attDocList.add(attDoc);
//								}
//						 }else{
							 attDoc = new AttDoc();
							 attDoc.setTypeName(String.valueOf(item.get("typeName")));
							 attDoc.setAttFieleName(String.valueOf(item.get("attFieleName")));
							 attDoc.setAttFilePath(url);
							 attDocList.add(attDoc);
//						 }
					 }
					 attDoc = null;
				 }
				 documentBean.setAttDocList(attDocList);
			 }
			 
			 if(jsonObject.containsKey("processIdeaList")){
				 processIdeaList = new ArrayList<ProcessIdea>();
				 JSONArray jsonArray = (JSONArray) jsonObject.get("processIdeaList");
				 for (int i = 0; i < jsonArray.size(); i++) {
					 processIdea = new ProcessIdea();
					 JSONObject item = (JSONObject) jsonArray.get(i);
					 processIdea.setJobStep((Long) item.get("JobStep"));
					 processIdea.setSignComment(String.valueOf(item.get("SignComment")));
					 processIdea.setSignTime(String.valueOf(item.get("SignTime")));
					 processIdea.setSignUserName(String.valueOf(item.get("SignUserName")));
					 processIdea.setSignUserPath(String.valueOf(item.get("SignUserPath")).replace("%3a", ":").replace("%2f", "/"));
					 processIdeaList.add(processIdea);
				}
				 documentBean.setProcessIdeaList(processIdeaList);
			 }
			 
			arrayList.add(documentBean);
			resultData.setResultList(arrayList);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		return resultData;
	}
	
	public static  ResultData  getBussinessTrip(String response, Context mContext){
		if (response != null && !"".equals(response)) {
		} else {
			return null;
		}
		ResultData resultData = new ResultData();
		ArrayList arrayList = new ArrayList();
		try {
			JSONParser parser = new JSONParser();
			JSONObject jsonObject = (JSONObject) parser.parse(response);
			JSONObject result = (JSONObject) jsonObject.get("result");
			if (!"".equals(result.get("message"))) {
				arrayList.add(result.get("message"));
				resultData.setResultList(arrayList);
				return resultData;
			}
			
			 BusinessTripBean  businessTripBean = new BusinessTripBean();
			 KqglBasic kqglBasic = null;
			 ArrayList<ProcessIdea> processIdeaList = null ;
			 ProcessIdea processIdea = null;
			 if(jsonObject.containsKey("InsID")){
				 businessTripBean.setInsID(String.valueOf(jsonObject.get("InsID")));
			 }
			 if(jsonObject.containsKey("btnState")){
				 businessTripBean.setBtnState(String.valueOf((Long)jsonObject.get("btnState")));
			 }
			 if(jsonObject.containsKey("btnStop")){
				 businessTripBean.setBtnStop(String.valueOf(jsonObject.get("btnStop")));
			 }
			 if(jsonObject.containsKey("KqglBasic")){
				 kqglBasic = new KqglBasic();
				 JSONObject item = (JSONObject) jsonObject.get("KqglBasic");
				 if(item.containsKey("EvectionUserName")){
					 kqglBasic.setEvectionUserName(String.valueOf(item.get("EvectionUserName")));
				 }
				 if(item.containsKey("EvectionDeport")){
					 kqglBasic.setEvectionDeport(String.valueOf(item.get("EvectionDeport")));
				 }
				 if(item.containsKey("EvectionStartTime")){
					 kqglBasic.setEvectionStartTime(String.valueOf(item.get("EvectionStartTime")));
				 }
				 if(item.containsKey("EvectionEndTime")){
					 kqglBasic.setEvectionEndTime(String.valueOf(item.get("EvectionEndTime")));
				 }
				 if(item.containsKey("EvectionAddress")){
					 kqglBasic.setEvectionAddress(String.valueOf(item.get("EvectionAddress")));
				 }
				 if(item.containsKey("EvectionReason")){
					 kqglBasic.setEvectionReason(String.valueOf(item.get("EvectionReason")));
				 }
				 if(item.containsKey("EvectionRideLine")){
					 kqglBasic.setEvectionRideLine(String.valueOf(item.get("EvectionRideLine")));
				 }
				 
				 businessTripBean.setKqglBasic(kqglBasic);
			 }
			 		 
			 if(jsonObject.containsKey("KqglProcessList")){
				 processIdeaList = new ArrayList<ProcessIdea>();
				 JSONArray jsonArray = (JSONArray) jsonObject.get("KqglProcessList");
				 for (int i = 0; i < jsonArray.size(); i++) {
					 processIdea = new ProcessIdea();
					 JSONObject item = (JSONObject) jsonArray.get(i);
					 processIdea.setJobStep(Long.valueOf(String.valueOf(item.get("JobStep"))));
					 processIdea.setSignComment(String.valueOf(item.get("SignComment")));
					 processIdea.setSignTime(String.valueOf(item.get("SignTime")));
					 processIdea.setSignUserName(String.valueOf(item.get("SignUserName")));
					 processIdea.setSignUserPath(String.valueOf(item.get("SignUserPath")).replace("%3a", ":").replace("%2f", "/"));
					 processIdeaList.add(processIdea);
				}
				 businessTripBean.setProcessIdeaList(processIdeaList);
			 }
			 
			arrayList.add(businessTripBean);
			resultData.setResultList(arrayList);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		return resultData;
	}
	
	public static  ResultData  getSubmitBussinessTrip(String response, Context mContext){
		if (response != null && !"".equals(response)) {
		} else {
			return null;
		}
		ResultData resultData = new ResultData();
		ArrayList arrayList = new ArrayList();
		try {
			JSONParser parser = new JSONParser();
			JSONObject jsonObject = (JSONObject) parser.parse(response);
			JSONObject result = (JSONObject) jsonObject.get("result");
			if (!"".equals(result.get("message"))) {
				arrayList.add(result.get("message"));
				resultData.setResultList(arrayList);
				return resultData;
			}
			SubmitBusinessTripBean submitBusinessTripBean = new SubmitBusinessTripBean();
			arrayList.add(submitBusinessTripBean);
			resultData.setResultList(arrayList);
			} catch (Exception e) {
				e.printStackTrace();
				return null;
		}
		return resultData;
	}
	
	
	public static  ResultData  getLeaderShipAgendaList(String response, Context mContext){
		if (response != null && !"".equals(response)) {
		} else {
			return null;
		}
		ResultData resultData = new ResultData();
		ArrayList arrayList = new ArrayList();
		try {
			JSONParser parser = new JSONParser();
			JSONObject jsonObject = (JSONObject) parser.parse(response);
			JSONObject result = (JSONObject) jsonObject.get("result");
			if (!"".equals(result.get("message"))) {
				arrayList.add(result.get("message"));
				resultData.setResultList(arrayList);
				return resultData;
			}
			AgendaBean agendaBean = new AgendaBean();
			ArrayList<Agenda> agendaList = null;
			if(jsonObject.containsKey("account")){
				agendaBean.setAccount((Long)jsonObject.get("account"));
			}
			if(jsonObject.containsKey("weekOfYear")){
				agendaBean.setWeek((Long)jsonObject.get("weekOfYear"));
			}
			if(jsonObject.containsKey("weekInfo")){
				agendaBean.setWeekDate(String.valueOf(jsonObject.get("weekInfo")));
			}
			
			if(jsonObject.containsKey("list")){
				JSONArray jsonList = (JSONArray) jsonObject.get("list");
				if(jsonList!=null&&jsonList.size()>0){
					agendaList = new ArrayList<Agenda>();
					Agenda  agenda = null;
					for (int i = 0; i < jsonList.size(); i++) {
						agenda = new Agenda();
						JSONObject jsonItem = (JSONObject) jsonList.get(i);
						if(jsonItem!=null&&jsonItem.containsKey("leader")){
							agenda.setLeader(String.valueOf(jsonItem.get("leader")));
							}
						if(jsonItem!=null&&jsonItem.containsKey("agent")){
							agenda.setAgent(String.valueOf(jsonItem.get("agent")));
							}
						if(jsonItem!=null&&jsonItem.containsKey("content")){
							agenda.setContent(String.valueOf(jsonItem.get("content")));
							}
						if(jsonItem!=null&&jsonItem.containsKey("time")){
							agenda.setTime(String.valueOf(jsonItem.get("time")));
							}
						if(jsonItem!=null&&jsonItem.containsKey("address")){
							agenda.setAddress(String.valueOf(jsonItem.get("address")));
							}
						agendaList.add(agenda);
					}
					agendaBean.setAgendaList(agendaList);
				}
			}
			
			arrayList.add(agendaBean);
			resultData.setResultList(arrayList);
			} catch (Exception e) {
				e.printStackTrace();
				return null;
		}
		return resultData;
	}
}
